import { definePropType } from '@/utils'

export interface TreeSelectFields {
  value?: string
  label?: string
  children?: string
}

export default {
  data: definePropType<any>(Array),
  fields: {
    type: definePropType<Record<string, any>>(Object),
    default: {
      children: 'children',
      label: 'name',
      value: 'orgUnitId'
    }
  },
  modelValue: [String, Number, Array],
  collapseTags: {
    type: Boolean,
    default: true
  },
  maxCollapseTags: {
    type: Number,
    default: 3
  },
  multiple: {
    type: Boolean,
    default: true
  }
}

export const emit = {
  'update:modelValue': (data: any) => !!data
}

export const buildTreeKeys = (configKeys: TreeSelectFields, origin: any[]) => {
  const keys = Object.entries(configKeys)
  return origin.map((o) => {
    keys.forEach(([k, v]) => (o[k] = o[v]))
    if (Array.isArray(o.children) && o.children.length) {
      o.children = buildTreeKeys(configKeys, o.children)
    }
    return o
  })
}
